var api = require('../../../utils/api.js')
var util = require('../../../utils/util.js');
var app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    services: [],
    shopLists: [],
    hiddenNo_Data_Image: true,
    showMask: false,
    indicatorDots: true,
    autoplay: false,
    currentPage: 0,
    current: 0,
    shareItem: [],
    HEIGHT: 0

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getWaiterList();
    this.getShopList();
    var that = this;

    // 隐藏分享
    wx.hideShareMenu()

    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          HEIGHT: res.windowHeight
        })
      },
    })
  },

  qrcode: function () {
    var shares = this.data.shareItem;
    shares.splice(0, shares.length);
    var i, myArr = this.data.shopLists;
    // console.log(myArr);
    for (var i = 0; i < myArr.length; i++) {
      var qrocde = myArr[i];
      var shopuserid = qrocde.shop_user_id;
      var shopid = qrocde.shop_id;
      var shopdiscount = qrocde.discount;
      var linkCode = qrocde.linkCode;
      var is_open = qrocde.is_open;
      var content = "/pages/center/inviteWaiter/inviteWaiter?shop_id=" + shopid + "&discount=" + shopdiscount + "&shopuserid=" + shopuserid + "&linkCode=" + linkCode + "&is_open=" + is_open;
      shares.push(content);
    };
    this.setData({
      shareItem: shares,
    });
  },
  // 打折权限
  switchChange: function (e) {
    var model = e.currentTarget.dataset.shop;
    var i, myArr = this.data.shopLists;
    if (model.discount == '2') {
      model.discount = '1';
    } else {
      model.discount = '2';
    }
    var result = [];
    for (var i = 0; i < myArr.length; i++) {
      var item = myArr[i];
      if (model.id == item.id) {
        result.push(model);
      } else {
        result.push(item);
      }
    };

    this.setData({
      shopLists: result
    })

  },
  // 开门权限
  openswitchChange: function (e) {
    var model = e.currentTarget.dataset.shop;
    var i, myArr = this.data.shopLists;
    if (model.is_open == '2') {
      model.is_open = '1';
    } else {
      model.is_open = '2';
    }
    var result = [];
    for (var i = 0; i < myArr.length; i++) {
      var item = myArr[i];
      if (model.id == item.id) {
        result.push(model);
      } else {
        result.push(item);
      }
    }
    this.setData({
      shopLists: result
    })
  },

  // 获取服务员列表
  getWaiterList: function () {
    var that = this;
    var params = new Object()
    api.POST({
      params: params,
      url: app.globalData.waiterList_geturl,
      success: function (res) {
        util.hiddenLoading();
        var temp = res.data.data;
        that.setData({
          hiddenNo_Data_Image: false
        })
        var services = (temp.result == undefined) ? [] : temp.result;
        that.setData({
          services: services
        })
      },
      fail: function (fail) {
        util.showError("网络错误，请检查网络。");
      }
    })
  },

  getShopList: function () {
    var that = this;
    var params = new Object()
    util.showLoading("加载中...");
    api.POST({
      params: params,
      url: app.globalData.waiter_shoplist_url,
      success: function (res) {
        util.hiddenLoading();
        var temp = res.data.data;
        that.setData({
          shopLists: temp,
          indicatorDots: temp.length != 1
        })
      },
      fail: function (fail) {
        util.showError("网络错误，请检查网络。");
      }
    })
  },

  // 处理服务员操作
  delOrCancelMethod: function (e) {
    var userInfo = e.currentTarget.dataset.currentUser;
    var that = this;
    wx.showModal({
      title: '提示',
      content: userInfo.flag == 1 ? '确定撤销？' : '确定删除?',
      success: function (res) {
        if (res.confirm) {
          that.waiterAuditMethod(userInfo.flag, 0, 0, userInfo);
        }
      }
    })
  },

  waiterAuditMethod: function (flag, discount, isopen, userInfo) {
    var that = this;
    var params = new Object()
    params.id = userInfo.id;//申请列表 服务员ID
    if (flag > 0) {
      params.flag = flag == 1 ? 2 : 3; //2：撤销 3：删除
    } else if (discount > 0) {
      params.discount = discount;//1：无打折权限 2：有打折权限
    } else if (isopen > 0) {
      params.is_open = isopen;
    }

    util.showLoading("处理中...");
    api.POST({
      params: params,
      url: app.globalData.waiter_audit_url,
      success: function (res) {
        util.hiddenLoading();
        that.getWaiterList();
      },
      fail: function (fail) {
        util.hiddenLoading();
        util.showError("网络错误，请检查网络。");
      }
    })
  },

  discountSwitchChange: function (e) {
    var userInfo = e.currentTarget.dataset.currentUser;
    //	1：无打折权限 2：有打折权限
    var discount = 0;
    if (e.detail.value == true) {
      discount = 2;
    } else {
      discount = 1
    }
    this.waiterAuditMethod(0, discount, 0, userInfo);
  },

  openDoorSwitchChange: function (e) {
    var userInfo = e.currentTarget.dataset.currentUser;
    //1：无开门权限 2：有开门权限
    var isOpen = 0;
    if (e.detail.value == true) {
      isOpen = 2;
    } else {
      isOpen = 1
    }
    this.waiterAuditMethod(0, 0, isOpen, userInfo);
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    this.qrcode();
    var path = this.data.shareItem[this.data.currentPage];
    var that = this;
    return {
      title: '邀请',
      path: path,
      imageUrl: "/images/ic_share_pic.png",
      success: function (res) {
        // 分享成功
        that.setData({
          showMask: false,
        });
        that.getWaiterList();
      },
      fail: function (res) {
        // 分享失败
      }
    }
  },

  setModalStatus: function (e) {
    var that = this;
    this.setData({
      showMask: !that.data.showMask
    })
  },

  // 阻止冒泡
  clickContent: function () {

  },

  // 扫码滚动事件
  swiperChange: function (e) {
    this.setData({
      currentPage: e.detail["current"]
    })
  },

  // 扫码左滑事件
  leftClick: function () {
    if (this.data.currentPage == 0) {
      return;
    }
    var that = this;
    that.setData({
      current: this.data.currentPage - 1
    })
  },

  // 扫码右滑事件
  rightClick: function () {
    if (this.data.shareItem.length - 1 == this.data.currentPage) {
      return;
    }
    var that = this;
    that.setData({
      current: this.data.currentPage + 1
    })
  },
})